package com.wzt.lianfirecontrol.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.MainActivity;
import com.wzt.lianfirecontrol.activity.WebActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;

public class NotifyUtils {

    public static final String notificationId = "com.xh.hdrm";

    public static final String notificationName = "com.xh.hdrm.notice";

    public static void createNotifition(Context context, BannerModel bannerModel) {
        if (null == bannerModel){
            return;
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        //创建NotificationChannel

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);

            notificationManager.createNotificationChannel(channel);

        }

        notificationManager.notify(1, getNotification(context, bannerModel));
    }

    public static Notification getNotification(Context context, BannerModel bannerModel) {
        String title = "智能运维";
        String content = "智能运维正在运行...";
        if (!StringUtils.isEmpty(bannerModel.getTitle())){
            title = bannerModel.getTitle();
        }
        if (!StringUtils.isEmpty(bannerModel.getViceTitle())){
            content = bannerModel.getViceTitle();
        }
        Notification.Builder builder = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.app_icon)
                .setContentTitle(title)
                .setContentText(content);

        //设置Notification的ChannelID,否则不能正常显示

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(notificationId);
        }
        if (context instanceof MainActivity) {
            Intent intent = new Intent(context, WebActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(ConstData.URL, bannerModel.getActionValue());
            intent.putExtras(bundle);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);
        }
        Notification notification = builder.build();

        return notification;

    }
}
